home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / src.lha / src / amitcp / sys / socketvar.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  9.5 KB  |  262 lines

  1. /*
  2.  * $Id: socketvar.h,v 1.15 1993/06/04 11:16:15 jraja Exp $
  3.  *
  4.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  5.  *                    Helsinki University of Technology, Finland.
  6.  *                    All rights reserved.
  7.  *
  8.  * HISTORY
  9.  * $Log: socketvar.h,v $
  10.  * Revision 1.15  1993/06/04  11:16:15  jraja
  11.  * Fixes for first public release.
  12.  *
  13.  * Revision 1.14  1993/05/30  08:30:42  too
  14.  * Changed struct selitem to struct newselitem
  15.  *
  16.  * Revision 1.13  1993/05/17  01:02:04  ppessi
  17.  * Changed RCS version
  18.  *
  19.  * Revision 1.12  1993/04/24  22:52:52  jraja
  20.  * Removed #ifdef USECLUSTERS (now using clusters always)
  21.  *
  22.  * Revision 1.11  93/04/12  09:22:35  09:22:35  jraja (Jarno Tapio Rajahalme)
  23.  * Changed sp_pgid to struct SocketBase *.
  24.  * 
  25.  * Revision 1.10  93/04/11  22:22:38  22:22:38  jraja (Jarno Tapio Rajahalme)
  26.  * Added parenthesis around && inside || for clarity.
  27.  * 
  28.  * Revision 1.9  93/03/03  16:12:41  16:12:41  jraja (Jarno Tapio Rajahalme)
  29.  * Cleanup. Changed u_long sb_max to extern. Removed sonewconn macro.
  30.  * 
  31.  * Revision 1.8  93/02/26  16:30:08  16:30:08  jraja (Jarno Tapio Rajahalme)
  32.  * fixed include and changed proc to selitem.
  33.  * 
  34.  * Revision 1.7  93/02/25  15:03:38  15:03:38  too (Tomi Ollila)
  35.  *  2 spaces removed 
  36.  * 
  37.  * Revision 1.6  93/02/24  16:26:27  16:26:27  too (Tomi Ollila)
  38.  * Changed so_linger and sb_timeout types from short to struct timeval
  39.  * 
  40.  * Revision 1.5  93/02/24  16:12:58  16:12:58  too (Tomi Ollila)
  41.  * returned SockatBase pointer.. one can use NULL here.
  42.  * 
  43.  * Revision 1.4  93/02/24  16:04:43  16:04:43  too (Tomi Ollila)
  44.  * removed SocketBase pointer.. too many problems occurred.
  45.  * 
  46.  * Revision 1.3  93/02/23  11:35:44  11:35:44  too (Tomi Ollila)
  47.  * added SocketBase pointer to sblock so sb_lock can use AMITCP tsleep code
  48.  * 
  49.  * Revision 1.2  93/02/04  18:17:15  18:17:15  jraja (Jarno Tapio Rajahalme)
  50.  * Added #ifdef USECLUSTERS to remove mbuf cluster dependant code.
  51.  * 
  52.  * Revision 1.1  92/11/20  15:42:39  15:42:39  jraja (Jarno Tapio Rajahalme)
  53.  * Initial revision
  54.  * 
  55.  *
  56.  */
  57.  
  58. /*-
  59.  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
  60.  * All rights reserved.
  61.  *
  62.  * Redistribution and use in source and binary forms, with or without
  63.  * modification, are permitted provided that the following conditions
  64.  * are met:
  65.  * 1. Redistributions of source code must retain the above copyright
  66.  *    notice, this list of conditions and the following disclaimer.
  67.  * 2. Redistributions in binary form must reproduce the above copyright
  68.  *    notice, this list of conditions and the following disclaimer in the
  69.  *    documentation and/or other materials provided with the distribution.
  70.  * 3. All advertising materials mentioning features or use of this software
  71.  *    must display the following acknowledgement:
  72.  *    This product includes software developed by the University of
  73.  *    California, Berkeley and its contributors.
  74.  * 4. Neither the name of the University nor the names of its contributors
  75.  *    may be used to endorse or promote products derived from this software
  76.  *    without specific prior written permission.
  77.  *
  78.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  79.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  80.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  81.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  82.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  83.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  84.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  85.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  86.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  87.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  88.  * SUCH DAMAGE.
  89.  *
  90.  *    @(#)socketvar.h    7.17 (Berkeley) 5/5/91
  91.  */
  92.  
  93. #ifndef SYS_SOCKETVAR_H
  94. #define SYS_SOCKETVAR_H
  95.  
  96. #ifndef SYS_TIME_H
  97. #include <sys/time.h>
  98. #endif
  99.  
  100. struct newselitem;
  101.  
  102. /*
  103.  * Kernel structure per socket.
  104.  * Contains send and receive buffer queues,
  105.  * handle on protocol and pointer to protocol
  106.  * private data and error information.
  107.  */
  108. struct socket {
  109.     short    so_type;        /* generic type, see socket.h */
  110.     short    so_options;        /* from socket call, see socket.h */
  111.     struct timeval so_linger;    /* time to linger while closing */
  112.     short    so_state;        /* internal state flags SS_*, below */
  113.     short   so_refcnt;              /* reference count for this socket */
  114.     caddr_t    so_pcb;            /* protocol control block */
  115.     struct    protosw *so_proto;    /* protocol handle */
  116. /*
  117.  * Variables for connection queueing.
  118.  * Socket where accepts occur is so_head in all subsidiary sockets.
  119.  * If so_head is 0, socket is not related to an accept.
  120.  * For head socket so_q0 queues partially completed connections,
  121.  * while so_q is a queue of connections ready to be accepted.
  122.  * If a connection is aborted and it has so_head set, then
  123.  * it has to be pulled out of either so_q0 or so_q.
  124.  * We allow connections to queue up based on current queue lengths
  125.  * and limit on number of queued connections for this socket.
  126.  */
  127.     struct    socket *so_head;    /* back pointer to accept socket */
  128.     struct    socket *so_q0;        /* queue of partial connections */
  129.     struct    socket *so_q;        /* queue of incoming connections */
  130.     short    so_q0len;        /* partials on so_q0 */
  131.     short    so_qlen;        /* number of connections on so_q */
  132.     short    so_qlimit;        /* max number queued connections */
  133.     short    so_timeo;        /* connection timeout */
  134.     u_short    so_error;        /* error affecting connection */
  135.     struct  SocketBase *so_pgid;    /* owner for signals */
  136.     u_long    so_oobmark;        /* chars to oob mark */
  137. /*
  138.  * Variables for socket buffering.
  139.  */
  140.     struct    sockbuf {
  141.         u_long    sb_cc;             /* actual chars in buffer */
  142.         u_long    sb_hiwat;         /* max actual char count */
  143.         u_long    sb_mbcnt;         /* chars of mbufs used */
  144.         u_long    sb_mbmax;         /* max chars of mbufs to use */
  145.         long    sb_lowat;         /* low water mark */
  146.         struct    mbuf *sb_mb;         /* the mbuf chain */
  147.         struct    newselitem *sb_sel;  /* chain for selecting tasks */
  148.         short    sb_flags;         /* flags, see below */
  149.         struct timeval sb_timeo;     /* timeout for read/write */
  150.     } so_rcv, so_snd;
  151. #define    SB_MAX        (64*1024)    /* default for max chars in sockbuf */
  152. #define    SB_LOCK        0x01        /* lock on data queue */
  153. #define    SB_WANT        0x02        /* someone is waiting to lock */
  154. #define    SB_WAIT        0x04        /* someone is waiting for data/space */
  155. #define    SB_SEL        0x08        /* someone is selecting */
  156. #define    SB_ASYNC    0x10        /* ASYNC I/O, need signals */
  157. #define    SB_NOTIFY    (SB_WAIT|SB_SEL|SB_ASYNC)
  158. #define    SB_COLL        0x20        /* collision selecting */
  159. #define    SB_NOINTR    0x40        /* operations not interruptible */
  160.  
  161.     caddr_t    so_tpcb;        /* Wisc. protocol control block XXX */
  162. };
  163.  
  164. /*
  165.  * Socket state bits.
  166.  */
  167. #define    SS_NOFDREF        0x001    /* no file table ref any more */
  168. #define    SS_ISCONNECTED        0x002    /* socket connected to a peer */
  169. #define    SS_ISCONNECTING        0x004    /* in process of connecting to peer */
  170. #define    SS_ISDISCONNECTING    0x008    /* in process of disconnecting */
  171. #define    SS_CANTSENDMORE        0x010    /* can't send more data to peer */
  172. #define    SS_CANTRCVMORE        0x020    /* can't receive more data from peer */
  173. #define    SS_RCVATMARK        0x040    /* at mark on input */
  174.  
  175. #define    SS_PRIV            0x080    /* privileged for broadcast, raw... */
  176. #define    SS_NBIO            0x100    /* non-blocking ops */
  177. #define    SS_ASYNC        0x200    /* async i/o notify */
  178. #define    SS_ISCONFIRMING        0x400    /* deciding to accept connection req */
  179.  
  180.  
  181. /*
  182.  * Macros for sockets and socket buffering.
  183.  */
  184.  
  185. /*
  186.  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
  187.  * This is problematical if the fields are unsigned, as the space might
  188.  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
  189.  * overflow and return 0.  Should use "lmin" but it doesn't exist now.
  190.  */
  191. #define    sbspace(sb) \
  192.     ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
  193.      (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
  194.  
  195. /* do we have to send all at once on a socket? */
  196. #define    sosendallatonce(so) \
  197.     ((so)->so_proto->pr_flags & PR_ATOMIC)
  198.  
  199. /* can we read something from so? */
  200. #define    soreadable(so) \
  201.     ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
  202.     ((so)->so_state & SS_CANTRCVMORE) || \
  203.     (so)->so_qlen || (so)->so_error)
  204.  
  205. /* can we write something to so? */
  206. #define    sowriteable(so) \
  207.     ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
  208.       (((so)->so_state&SS_ISCONNECTED) || \
  209.        ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) \
  210.      || ((so)->so_state & SS_CANTSENDMORE) \
  211.      || (so)->so_error)
  212.  
  213. /* adjust counters in sb reflecting allocation of m */
  214. #define    sballoc(sb, m) { \
  215.     (sb)->sb_cc += (m)->m_len; \
  216.     (sb)->sb_mbcnt += MSIZE; \
  217.     if ((m)->m_flags & M_EXT) \
  218.         (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
  219. }
  220.  
  221. /* adjust counters in sb reflecting freeing of m */
  222. #define    sbfree(sb, m) { \
  223.     (sb)->sb_cc -= (m)->m_len; \
  224.     (sb)->sb_mbcnt -= MSIZE; \
  225.     if ((m)->m_flags & M_EXT) \
  226.         (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
  227. }
  228.  
  229. /*
  230.  * Set lock on sockbuf sb; sleep if lock is already held.
  231.  * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
  232.  * Returns error without lock if sleep is interrupted.
  233.  */
  234. #define sblock(sb, p) ((sb)->sb_flags & SB_LOCK ? sb_lock(sb, p) : \
  235.         ((sb)->sb_flags |= SB_LOCK, 0))
  236.  
  237. /* release lock on sockbuf sb */
  238. #define    sbunlock(sb) { \
  239.     (sb)->sb_flags &= ~SB_LOCK; \
  240.     if ((sb)->sb_flags & SB_WANT) { \
  241.         (sb)->sb_flags &= ~SB_WANT; \
  242.         wakeup((caddr_t)&(sb)->sb_flags); \
  243.     } \
  244. }
  245.  
  246. #define    sorwakeup(so)    sowakeup((so), &(so)->so_rcv)
  247. #define    sowwakeup(so)    sowakeup((so), &(so)->so_snd)
  248.  
  249. #ifdef KERNEL
  250. extern u_long    sb_max;
  251.  
  252. /* strings for sleep message: */
  253. extern    char netio[], netcon[], netcls[];
  254.  
  255. #endif
  256. #endif /* !SYS_SOCKETVAR_H */
  257.  
  258.  
  259.  
  260.  
  261.  
  262.